package com.example.myapplication.tack_20.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.tack_20.adapter.ApplicationAdapter;
import com.example.myapplication.tack_20.data.ServletData;
import com.example.myapplication.utils.OkHttpTool;

import java.util.List;


public class ServletFragment extends Fragment {
    private View view;
    private List<ServletData.RowsDTO> servletList;
    private Handler handler;
    private RecyclerView servlet;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.tack_20_fragment_servlet, container, false);
        init();
        getBM();
        return view;

    }

    private void getBM() {
        String url = "prod-api/api/volunteer-service/activity/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ServletData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ServletData servletData = (ServletData)obj;
                if(servletData.getCode() == 200){
                    servletList = servletData.getRows();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            BMAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void BMAdapter() {
        ApplicationAdapter applicationAdapter = new ApplicationAdapter(servletList, ServletFragment.this.getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ServletFragment.this.getContext());
        servlet.setLayoutManager(linearLayoutManager);
        servlet.setAdapter(applicationAdapter);
    }

    private void init() {
        handler = new Handler();
        servlet = view.findViewById(R.id.servlet);
    }
}